package com.cqgcxy.shopping.controller;
import java.io.IOException;
import java.sql.ResultSet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cqgcxy.shopping.common.CommException;
import com.cqgcxy.shopping.domain.Shop;
import com.cqgcxy.shopping.service.UserService;

@WebServlet(urlPatterns="/shop.do")


public class ShopServlet extends HttpServlet{

	private static final long serialVersionUID = -6204376603444672194L;
	private Logger  logger = LoggerFactory.getLogger(getClass());
	private UserService userService=new UserService();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		logger.info("1111");
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		logger.info("接受到shop.do.dopost");
		
			
			
		try {
			// 1.抽取参数，封装为实体对象
			
			String shop_name="2017玉石摆件客厅装饰品玄关柜招财工艺品摆设";  //商品的名称
		//	shop_name = shop_name.replaceAll(" ", ""); //去掉商品的名称中的空格
			double shop_price=268.00;//商品的价格
			
			String shop_yanse=req.getParameter("shop_yanse");   //商品的颜色
			String shop_gaodu=req.getParameter("shop_gaodu");//商品的高度
			int shop_shuliang=Integer.parseInt(req.getParameter("shop_shuliang"));//商品的数量
			
			if(shop_shuliang>100) {
				shop_shuliang=100;//如果超过库存变成100
			}	

			logger.debug("验证 1  获取参数成功：商品的名称：{},商品的价格：{},商品的颜色：{},商品的高度：{},商品的数量：{}",shop_name,shop_price,shop_yanse,shop_gaodu,shop_shuliang);		
			
			// 2.类型转换


			//4.参数封装为对象
			Shop shop = new Shop();

			shop.setShop_name(shop_name);
			shop.setShop_price(shop_price);
			shop.setShop_yanse(shop_yanse);
			shop.setShop_gaodu(shop_gaodu);
			shop.setShop_shuliang(shop_shuliang);
			

			
			// 5.调用业务方法
			
			HttpSession session=req.getSession();
			session.setAttribute("shop", shop);
			logger.debug("业务处理成功");
			userService.shop(shop_name,shop_price,shop_yanse,shop_gaodu,shop_shuliang);
			// 6.转发或者重定向
			
			//resp.sendRedirect(req.getContextPath()+"/member/cart.jsp");//重定向
			req.getRequestDispatcher("/member/cart.jsp").forward(req, resp);

			logger.debug("修改个人信息成功");   
			
		} catch (Exception e) {
			/*req.setAttribute("error", e.getMessage());
			req.getRequestDispatcher("/login.jsp").forward(req, resp);
			logger.info("登录失败:{}",e.getMessage());*/

			req.getRequestDispatcher("/login.jsp").forward(req, resp);
			logger.info("修改a 失败:{}",e.getMessage());
		}
		
	}
}
